package com.wuye.rcgl.service.impl;

import java.util.List;

import com.wuye.basic.domain.WyCommunity;
import com.wuye.rcgl.mapper.WyFixmanCommunityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wuye.rcgl.mapper.WyFixmanMapper;
import com.wuye.rcgl.domain.WyFixman;
import com.wuye.rcgl.service.IWyFixmanService;
import com.wuye.basic.mapper.WyCommunityMapper;

/**
 * 维修员Service业务层处理
 * 
 * @author wpc
 * @date 2024-11-22
 */
@Service
public class WyFixmanServiceImpl implements IWyFixmanService 
{
    @Autowired
    private WyFixmanMapper wyFixmanMapper;

    @Autowired
    private WyFixmanCommunityMapper wyFixmanCommunityMapper;

    @Autowired
    private WyCommunityMapper wyCommunityMapper;
    /**
     * 查询维修员
     * 
     * @param fixmanId 维修员主键
     * @return 维修员
     */
    @Override
    public WyFixman selectWyFixmanByFixmanId(Long fixmanId)
    {
        return wyFixmanMapper.selectWyFixmanByFixmanId(fixmanId);
    }

    /**
     * 查询维修员列表
     * 
     * @param wyFixman 维修员
     * @return 维修员
     */
    @Override
    public List<WyFixman> selectWyFixmanList(WyFixman wyFixman)
    {
        return wyFixmanMapper.selectWyFixmanList(wyFixman);
    }

    /**
     * 新增维修员
     * 
     * @param wyFixman 维修员
     * @return 结果
     */
    @Override
    public int insertWyFixman(WyFixman wyFixman)
    {

        // 插入维修员基本信息
        int result = wyFixmanMapper.insertWyFixman(wyFixman);
        // 获取维修员ID
        Long fixmanId = wyFixman.getFixmanId();
        // 获取维修员可以维护的小区ID列表
        List<Long> fixmanCommunityIds = wyFixman.getFixmanCommunityIds();
        if (fixmanCommunityIds != null){
            for (Long fixmanCommunityId : fixmanCommunityIds) {
                wyFixmanCommunityMapper.insertWyFixmanCommunity(fixmanId, fixmanCommunityId);
            }
        }
        return result;
//        return wyFixmanMapper.insertWyFixman(wyFixman);
    }

    /**
     * 修改维修员
     * 
     * @param wyFixman 维修员
     * @return 结果
     */
    @Override
//    @Transactional
    public int updateWyFixman(WyFixman wyFixman)
    {
        //
        List<Long> fixmanCommunityIds = wyFixman.getFixmanCommunityIds();
        //获取维修员ID
        Long fixmanId = wyFixman.getFixmanId();
        //删除维修员维护的小区
        wyFixmanCommunityMapper.deleteWyFixmanCommunity(fixmanId);
        //重新插入
        for (Long fixmanCommunityId : fixmanCommunityIds) {
            wyFixmanCommunityMapper.insertWyFixmanCommunity(fixmanId, fixmanCommunityId);
        }
        return wyFixmanMapper.updateWyFixman(wyFixman);
    }

    /**
     * 批量删除维修员
     * 
     * @param fixmanIds 需要删除的维修员主键
     * @return 结果
     */
    @Override
    public int deleteWyFixmanByFixmanIds(Long[] fixmanIds)
    {
        wyFixmanCommunityMapper.deleteWyFixmanCommunity(fixmanIds[0]);
        return wyFixmanMapper.deleteWyFixmanByFixmanIds(fixmanIds);
    }

    /**
     * 删除维修员信息
     * 
     * @param fixmanId 维修员主键
     * @return 结果
     */
    @Override
    public int deleteWyFixmanByFixmanId(Long fixmanId)
    {
        wyFixmanCommunityMapper.deleteWyFixmanCommunity(fixmanId);
        return wyFixmanMapper.deleteWyFixmanByFixmanId(fixmanId);
    }
}
